import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd';

declare var bpm: any;
declare var ui: any;
declare var form: any;
declare var security: any;

@Component({
  templateUrl: './security.equipment.info.bpm.component.html'
})
export class SecurityEquipmentInfoBpmComponent implements OnInit {

  ss: any;
  // 流程服务
  bs: any;
  // 表单服务
  fs: any;
  // 菜单
  menu = null;
  // 字典
  typeDic: any = null;
  modelDic: any = null;
  produceFactoryDic: any = null;
  positionDic: any = null;
  unitDic: any = null;
  regulatoryAuthorityDic: any = null;
  administrationDic: any = null;
  airportDic: any = null;
  // 出场年月
  outTime = null;
  // 启用年月
  useTime = null;

  constructor(public route: ActivatedRoute, private modalService: NzModalService) {

    this.ss = security.SecurityService;
    this.ss.show = false;
    // 注册表单
    this.fs = new form.FormSimpleService('securityequipment', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id']
    });
    // 注册流程
    this.bs = new bpm.BpmService('securityequipment', 'equipment_flow');
    // 注册菜单
    this.menu = new ui.TabService('bpm', 's');
    this.menu.setTab('main');
    // 注册字典
    this.typeDic = new form.DictionaryService('e_type');
    this.typeDic.loadAll();
    this.modelDic = new form.DictionaryService('e_model');
    this.modelDic.loadAll();
    this.produceFactoryDic = new form.DictionaryService('e_produce_factory');
    this.produceFactoryDic.loadAll();
    this.positionDic = new form.DictionaryService('e_position');
    this.positionDic.loadAll();
    this.unitDic = new form.DictionaryService('zd_airport');
    this.unitDic.loadAll();
    this.regulatoryAuthorityDic = new form.DictionaryService('e_regulatory_authority');
    this.regulatoryAuthorityDic.loadAll();
    this.administrationDic = new form.DictionaryService('e_administration');
    this.administrationDic.loadAll();
    this.airportDic = new form.DictionaryService('zd_airport');
    this.airportDic.loadAll();
  }

  ngOnInit() {
    this.initData();
  }

  initData() {
    let id = '-1';
    this.route.queryParams.subscribe((res) => {
      if (res.id) {
        id = res.id;
      }
    });


    this.fs.editRowByKey(id, (data) => {

      if (data.useUnit === undefined || data.useUnit === null) {
        data.useUnit = this.ss.user.userDepCode[0];
      }
      if (data.FIELD_ISNEW) {

      } else {
        this.outTime = new Date(data.outYear + '-' + data.outMonth);
        this.useTime = new Date(data.useYear + '-' + data.useMonth);
      }
      // 设置流程数据
      debugger
      this.bs.dataKey = data.id;
      this.bs.data = data;
      // 进来先查询流程信息
      this.bs.findWork();

    });
  }

  setOutTime() {
    this.bs.data.outYear = this.outTime.getFullYear();
    this.bs.data.outMonth = this.outTime.getMonth() + 1;
  }

  setUseTime() {
    this.bs.data.useYear = this.useTime.getFullYear();
    this.bs.data.useMonth = this.useTime.getMonth() + 1;
  }

  close() {
    window.close();
  }

  loadHistory() {
    this.menu.setTab('history');
    this.bs.loadHistory();
  }

  loadImage() {
    this.menu.setTab('image');
    this.bs.loadImage();
  }
}
